{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Panel是3D容器的数据。面板数据一词来源于计量经济学。`Pandas: pan(el)-da(ta)-s`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3轴旨在给出描述涉及面板数据的操作的一些语义，它们是：\n",
    "- *items* - `axis0`, 每个项目对应于内部包含的数据帧(DataFrame);\n",
    "- *major_axis* - `axis1`, 它是每个数据帧(DataFrame)的索引(行数)；\n",
    "- *minor_axis* - `axis2`, 它是每个数据帧(DataFrame)的列数。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# pandas.Panel()\n",
    "可是使用以下构造函数创建面板  \n",
    "`pandas.Panel(data,item,major_axis,minor_axis,dtype,copy)`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 创建Panel\n",
    "可以使用多种方式创建Panel  \n",
    "- 从ndarrays创建；\n",
    "- 从DataFrames的dict创建"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 从3D ndarray创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[0.42534476, 0.33858151, 0.29907772, 0.84807305, 0.02413152],\n",
       "        [0.75877935, 0.0886882 , 0.10006824, 0.07436327, 0.94838993],\n",
       "        [0.34890793, 0.93672558, 0.52361118, 0.47929   , 0.97141217],\n",
       "        [0.56055448, 0.36354071, 0.2937232 , 0.16015771, 0.61737252]],\n",
       "\n",
       "       [[0.47070543, 0.08451237, 0.37548926, 0.51826905, 0.29300974],\n",
       "        [0.13953843, 0.05880772, 0.20850527, 0.08441297, 0.76233336],\n",
       "        [0.30815607, 0.50081099, 0.48854048, 0.89815775, 0.77571685],\n",
       "        [0.49897902, 0.74270224, 0.98644531, 0.17870272, 0.0935906 ]]])"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "data = np.random.rand(2,4,5)\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\pcApp\\Anaconda3\\lib\\site-packages\\IPython\\core\\interactiveshell.py:2963: FutureWarning: \n",
      "Panel is deprecated and will be removed in a future version.\n",
      "The recommended way to represent these types of 3-dimensional data are with a MultiIndex on a DataFrame, via the Panel.to_frame() method\n",
      "Alternatively, you can use the xarray package http://xarray.pydata.org/en/stable/.\n",
      "Pandas provides a `.to_xarray()` method to help automate this conversion.\n",
      "\n",
      "  exec(code_obj, self.user_global_ns, self.user_ns)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<class 'pandas.core.panel.Panel'>\n",
       "Dimensions: 2 (items) x 4 (major_axis) x 5 (minor_axis)\n",
       "Items axis: 0 to 1\n",
       "Major_axis axis: 0 to 3\n",
       "Minor_axis axis: 0 to 4"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p = pd.Panel(data)\n",
    "p"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 从DataFrame对象的dict创建面板"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\pcApp\\Anaconda3\\lib\\site-packages\\IPython\\core\\interactiveshell.py:2963: FutureWarning: \n",
      "Panel is deprecated and will be removed in a future version.\n",
      "The recommended way to represent these types of 3-dimensional data are with a MultiIndex on a DataFrame, via the Panel.to_frame() method\n",
      "Alternatively, you can use the xarray package http://xarray.pydata.org/en/stable/.\n",
      "Pandas provides a `.to_xarray()` method to help automate this conversion.\n",
      "\n",
      "  exec(code_obj, self.user_global_ns, self.user_ns)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<class 'pandas.core.panel.Panel'>\n",
       "Dimensions: 2 (items) x 4 (major_axis) x 3 (minor_axis)\n",
       "Items axis: Item1 to Item2\n",
       "Major_axis axis: 0 to 3\n",
       "Minor_axis axis: 0 to 2"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "data = {'Item1': pd.DataFrame(np.random.randn(4,3)),\n",
    "       'Item2': pd.DataFrame(np.random.randn(4,2))}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Item1':           0         1         2\n",
       " 0 -0.759846 -1.836975 -0.635213\n",
       " 1 -1.332937  1.242657 -1.715113\n",
       " 2  3.139412 -0.904445 -1.786193\n",
       " 3 -0.966438  0.795359 -0.143093, 'Item2':           0         1\n",
       " 0 -0.420935 -0.860756\n",
       " 1  0.247987 -1.563487\n",
       " 2  0.444713 -1.601044\n",
       " 3 -1.765692 -1.065479}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\pcApp\\Anaconda3\\lib\\site-packages\\IPython\\core\\interactiveshell.py:2963: FutureWarning: \n",
      "Panel is deprecated and will be removed in a future version.\n",
      "The recommended way to represent these types of 3-dimensional data are with a MultiIndex on a DataFrame, via the Panel.to_frame() method\n",
      "Alternatively, you can use the xarray package http://xarray.pydata.org/en/stable/.\n",
      "Pandas provides a `.to_xarray()` method to help automate this conversion.\n",
      "\n",
      "  exec(code_obj, self.user_global_ns, self.user_ns)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<class 'pandas.core.panel.Panel'>\n",
       "Dimensions: 2 (items) x 4 (major_axis) x 3 (minor_axis)\n",
       "Items axis: Item1 to Item2\n",
       "Major_axis axis: 0 to 3\n",
       "Minor_axis axis: 0 to 2"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p = pd.Panel(data)\n",
    "p"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 创建一个空面板\n",
    "可以使用`Panel`的构造函数创建一个空面板，如下所示："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\pcApp\\Anaconda3\\lib\\site-packages\\IPython\\core\\interactiveshell.py:2963: FutureWarning: \n",
      "Panel is deprecated and will be removed in a future version.\n",
      "The recommended way to represent these types of 3-dimensional data are with a MultiIndex on a DataFrame, via the Panel.to_frame() method\n",
      "Alternatively, you can use the xarray package http://xarray.pydata.org/en/stable/.\n",
      "Pandas provides a `.to_xarray()` method to help automate this conversion.\n",
      "\n",
      "  exec(code_obj, self.user_global_ns, self.user_ns)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<class 'pandas.core.panel.Panel'>\n",
       "Dimensions: 0 (items) x 0 (major_axis) x 0 (minor_axis)\n",
       "Items axis: None\n",
       "Major_axis axis: None\n",
       "Minor_axis axis: None"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# creating an empty panel\n",
    "import pandas as pd\n",
    "\n",
    "p = pd.Panel()\n",
    "p"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 从面板中选择数据\n",
    "要从面板中选择数据，可以使用如下方式：  \n",
    "- Items \n",
    "- Major_axis\n",
    "- Minor_axis"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 使用Items选择数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Item1':           0         1         2\n",
       " 0  2.949558 -0.551728  2.576187\n",
       " 1  1.134747  0.381403 -0.212214\n",
       " 2  0.625462  0.522112  0.208644\n",
       " 3 -0.928474  2.260661 -0.967780, 'Item2':           0         1\n",
       " 0 -0.928751 -0.307884\n",
       " 1  1.110867 -1.709673\n",
       " 2 -1.899409 -1.740578\n",
       " 3 -0.465847 -0.280767}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "data = {'Item1' : pd.DataFrame(np.random.randn(4, 3)), \n",
    "        'Item2' : pd.DataFrame(np.random.randn(4, 2))}\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\pcApp\\Anaconda3\\lib\\site-packages\\IPython\\core\\interactiveshell.py:2963: FutureWarning: \n",
      "Panel is deprecated and will be removed in a future version.\n",
      "The recommended way to represent these types of 3-dimensional data are with a MultiIndex on a DataFrame, via the Panel.to_frame() method\n",
      "Alternatively, you can use the xarray package http://xarray.pydata.org/en/stable/.\n",
      "Pandas provides a `.to_xarray()` method to help automate this conversion.\n",
      "\n",
      "  exec(code_obj, self.user_global_ns, self.user_ns)\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2.949558</td>\n",
       "      <td>-0.551728</td>\n",
       "      <td>2.576187</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.134747</td>\n",
       "      <td>0.381403</td>\n",
       "      <td>-0.212214</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.625462</td>\n",
       "      <td>0.522112</td>\n",
       "      <td>0.208644</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-0.928474</td>\n",
       "      <td>2.260661</td>\n",
       "      <td>-0.967780</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          0         1         2\n",
       "0  2.949558 -0.551728  2.576187\n",
       "1  1.134747  0.381403 -0.212214\n",
       "2  0.625462  0.522112  0.208644\n",
       "3 -0.928474  2.260661 -0.967780"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p = pd.Panel(data)\n",
    "p['Item1']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 使用Major_axis选择数据\n",
    "可以使用`panel.major_xs(index)`方法访问数据。示例如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Item1':           0         1         2\n",
       " 0  2.949558 -0.551728  2.576187\n",
       " 1  1.134747  0.381403 -0.212214\n",
       " 2  0.625462  0.522112  0.208644\n",
       " 3 -0.928474  2.260661 -0.967780, 'Item2':           0         1\n",
       " 0 -0.928751 -0.307884\n",
       " 1  1.110867 -1.709673\n",
       " 2 -1.899409 -1.740578\n",
       " 3 -0.465847 -0.280767}"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Item1</th>\n",
       "      <th>Item2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.134747</td>\n",
       "      <td>1.110867</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.381403</td>\n",
       "      <td>-1.709673</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-0.212214</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      Item1     Item2\n",
       "0  1.134747  1.110867\n",
       "1  0.381403 -1.709673\n",
       "2 -0.212214       NaN"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 想象：两页数据叠放，选择两页数据的第2行\n",
    "p.major_xs(1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 使用Minor_axis选择数据\n",
    "可以使用panel.minor_xs(index)方法访问数据。示例代码："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Item1</th>\n",
       "      <th>Item2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-0.551728</td>\n",
       "      <td>-0.307884</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.381403</td>\n",
       "      <td>-1.709673</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.522112</td>\n",
       "      <td>-1.740578</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2.260661</td>\n",
       "      <td>-0.280767</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      Item1     Item2\n",
       "0 -0.551728 -0.307884\n",
       "1  0.381403 -1.709673\n",
       "2  0.522112 -1.740578\n",
       "3  2.260661 -0.280767"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 想象：两页数据叠放，选择两页数据的第2列\n",
    "p.minor_xs(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
